<?php
namespace app\index\controller;
use think\Controller;
use think\Db;

class Base extends Controller
{
    public function __construct(){
        parent::__construct();

        $login_user = session("cch_user");
        $action = strtolower(request()->action());
        if(empty($login_user) && !in_array($action, $this->exception)){
            // $this->redirect(url('user/login'));
        }

        // 获取导航分类
        // $c_order = 'sort desc,id asc';
        // $categorys = Db::name('category')->where([
        //     ['pid','=',0],
        //     ['status','=',1]
        // ])->order($c_order)->select();
        // foreach ($categorys as $k => $vo) {
        //     $categorys[$k]['attribute'] = explode(',',$vo['attribute']);
            
        //     $childs = Db::name('category')->where([
        //         ['pid','=',$vo['id']],
        //         ['status','=',1]
        //     ])->order($c_order)->select();
        //     $categorys[$k]['childs'] = $childs;
            
        //     if ($vo['name'] == '产品') {
        //         foreach ($childs as $c_k => $c_v) {
        //             $childs[$c_k]['attribute'] = explode(',',$c_v['attribute']);

        //             $products = Db::name('product')->field('id,title,min_description,attribute,price,tags')->where([
        //                 ['category_id','=',$c_v['id']]
        //             ])->select();
        //             foreach ($products as $p_k => $p_v) {
        //                 if (empty($p_v['attribute'])) {
        //                     $products[$p_k]['attribute'] = [];
        //                 }else{
        //                     $products[$p_k]['attribute'] = explode(',',$p_v['attribute']);
        //                 }
        //                 if (empty($p_v['tags'])) {
        //                     $products[$p_k]['tags'] = [];
        //                 }else{
        //                     $products[$p_k]['tags'] = explode(',',$p_v['tags']);
        //                 }
        //             }
        //             $childs[$c_k]['product'] = $products;
        //         }
        //         $pro_category = $categorys[$k];
        //         $pro_category['childs'] = $childs;
        //     }
        // }

        $tmp_configs = Db::name('config')->select();
        $configs = [];
        foreach ($tmp_configs as $k => $vo) {
            $configs[$vo['key']] = $vo['value'];
        }

        // 精选推荐
        // $tj_products = Db::name('product')->field('id,title,min_description,attribute,price,tags')->where([
        //     ['attribute','like','%2%']
        // ])->select();
        // foreach ($tj_products as $k => $vo) {
        //     if (empty($vo['attribute'])) {
        //         $tj_products[$k]['attribute'] = [];
        //     }else{
        //         $tj_products[$k]['attribute'] = explode(',',$vo['attribute']);
        //     }
        //     if (empty($vo['tags'])) {
        //         $tj_products[$k]['tags'] = [];
        //     }else{
        //         $tj_products[$k]['tags'] = explode(',',$vo['tags']);
        //     }
        // }

        // $login_user = Db::name('user')->where('id',1)->find();
        // $this->login_user = $login_user;
        $this->page_info = [
            'action'=>$action,
            'controller'=>strtolower(request()->controller()),
            'path'=>request()->path()
        ];
        $this->page_info['controller'] = explode('/', $this->page_info['path'])[1];
        $this->assign([
            'login_user'=>$login_user,
            'categorys'=>$categorys,
            'tj_products'=>$tj_products,
            'pro_category'=>$pro_category,
            'configs'=>$configs
        ]);
    }




}
